package com.example.forum.service.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.BoardMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.service.IBoardService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {

    @Autowired
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        if(num == null || num < 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }
        List<Board> list = boardMapper.selectByNum(num);
        if(list.size() < 0 || list.isEmpty()) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        return list;
    }

    @Override
    public Integer updateBoard(Long boardId) {
        if(boardId < 0) {
            log.warn("BoardId参数校验失败");
            throw new ApplicationException("BoardId参数校验失败");
        }

        Board board = boardMapper.selectByPrimaryKey(boardId);
        if(board == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        //更新board中的文章数
        board.setArticle_count(board.getArticle_count() - 1);
        Integer ret = boardMapper.updateByPrimaryKeySelective(board);
        if(ret < 0 || ret == null) {
            log.warn("删除文章失败");
            throw new ApplicationException("删除文章失败");
        }
        log.info("删除文章成功");
        return ret;

    }


    /**
     * 发表文章
     * @param article
     * @return
     */
    @Override
    public Integer updateBoardArticleCount(Article article) {
        if(article.getBoard_id() < 0) {
            log.warn("BoardId参数校验失败");
            throw new ApplicationException("BoardId参数校验失败");
        }

        Long boardId = article.getBoard_id();
        Board board = boardMapper.selectByPrimaryKey(boardId);
        if(board == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        //校验该板块目前是否可以进行言/是否被删除
        if(board.getState() == 1 || board.getDelete_state() == 1) {
            log.warn("当前模块被禁言或已删除");
            throw new ApplicationException("当前模块被禁言或已删除");
        }
        //更新board中的文章数
        board.setArticle_count(board.getArticle_count() + 1);
        Integer ret = boardMapper.updateByPrimaryKeySelective(board);
        if(ret < 0 || ret == null) {
            log.warn("发表文章失败");
            throw new ApplicationException("发表文章失败");
        }
        log.info("发表文章成功");
        return ret;

    }

    @Override
    public Board getBoardById(Long boardId) {
        Board board = boardMapper.selectByPrimaryKey(boardId);
        if(board == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        if(board.getDelete_state() == 1) {
            log.warn("当前board不存在");
            throw new ApplicationException("当前board不能存在");
        }
        log.info("根据boardId查找板块成功");
        return board;
    }
}
